# Multi-stage Dockerfile for QianXun-Server (Fastify + TypeScript)

# ---------- Builder stage ----------
FROM node:18-slim AS builder

WORKDIR /app

# Install dependencies first (use npm ci when lockfile exists)
COPY package*.json ./
RUN if [ -f package-lock.json ]; then npm ci; else npm install; fi

# Copy source and build
COPY tsconfig.json ./
COPY src ./src
RUN npm run build

# ---------- Runtime stage ----------
FROM node:18-slim AS runner

ENV NODE_ENV=production
ENV PORT=3000
WORKDIR /app

# Create logs directory
RUN mkdir -p /app/logs

# Copy production dependencies and built files
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY package*.json ./

EXPOSE 3000

CMD ["node", "dist/src/index.js"]